const repairDao = require('../dao/repair');

const Response = require('../entity/http/Response');

const repairService = {
    async addRepair(reqId, uid, { deviceId, time, description, processMethod, changeAccessory, repairman }) {
        let result;
        try {
            let dbResult = await repairDao.addRepair(reqId, uid, deviceId, time, description, processMethod, changeAccessory, repairman);
            if (dbResult.insertId) {
                result = new Response().success("新增维修信息成功");
            } else {
                result = new Response().fail(500, "新增维修信息失败");
            }
        } catch (err) {
            result = new Response().fail(500, "新增维修信息失败 请检查参数是否正确");
        }
        return result;
    },
    async getAllRepair(reqId, uid) {
        let result;
        try {
            let dbResult = await repairDao.getAllRepair(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "获取所有维修信息失败 请检查参数是否正确");
        }
        return result;
    },
    async updateRepair(reqId, uid, { id, time, description, processMethod, changeAccessory, repairman }) {
        let result;
        try {
            let dbResult = await repairDao.updateRepair(reqId, uid, id, time, description, processMethod, changeAccessory, repairman);
            if (dbResult.changedRows) {
                result = new Response().success("修改维修信息成功");
            } else {
                result = new Response().fail(500, "修改维修信息前后无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改维修信息失败 请检查参数是否正确");
        }
        return result;
    },
    async deleteRepair(reqId, uid, repair) {
        let result;
        try {
            let dbResult = await repairDao.delRepair(reqId, uid, repair);
            if (dbResult.changedRows) {
                result = new Response().success("删除维修信息成功");
            } else {
                result = new Response().fail(500, "该维修信息不存在");
            }
        } catch (err) {
            result = new Response().fail(500, "删除维修信息失败 请检查参数是否正确");
        }
        return result;
    }
};

module.exports = repairService;